using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace MT5Trade.Models.MarketData
{
    /// <summary>
    /// 缓存性能测试结果实体类
    /// 严格遵守 generic-rule.md 规则
    /// 对应数据库表：cache_performance_test
    /// </summary>
    [Table("cache_performance_test")]
    public class CachePerformanceTestEntity
    {
        [Key]
        [Column("id")]
        public long Id { get; set; }

        [Required]
        [Column("test_name")]
        [MaxLength(100)]
        public string TestName { get; set; }

        [Required]
        [Column("test_case_id")]
        [MaxLength(50)]
        public string TestCaseId { get; set; }

        [Required]
        [Column("operation_type")]
        [MaxLength(20)]
        public string OperationType { get; set; }

        [Required]
        [Column("data_count")]
        public int DataCount { get; set; }

        [Column("file_size_kb")]
        public decimal? FileSizeKb { get; set; }

        [Required]
        [Column("duration_ms")]
        public decimal DurationMs { get; set; }

        [Column("memory_used_mb")]
        public decimal? MemoryUsedMb { get; set; }

        [Column("cpu_usage_percent")]
        public decimal? CpuUsagePercent { get; set; }

        [Column("speedup_ratio")]
        public decimal? SpeedupRatio { get; set; }

        [Column("cache_hit_rate")]
        public decimal? CacheHitRate { get; set; }

        [Column("test_time")]
        public DateTime TestTime { get; set; }

        [Column("test_environment")]
        [MaxLength(100)]
        public string TestEnvironment { get; set; }

        /// <summary>
        /// 创建性能测试记录
        /// </summary>
        public static CachePerformanceTestEntity Create(
            string testName,
            string testCaseId,
            string operationType,
            int dataCount,
            decimal durationMs,
            string environment = "LOCAL_DEV")
        {
            return new CachePerformanceTestEntity
            {
                TestName = testName,
                TestCaseId = testCaseId,
                OperationType = operationType,
                DataCount = dataCount,
                DurationMs = durationMs,
                TestTime = DateTime.UtcNow,
                TestEnvironment = environment
            };
        }

        /// <summary>
        /// 计算加速比
        /// </summary>
        public void CalculateSpeedup(decimal baselineDuration)
        {
            if (DurationMs > 0)
            {
                SpeedupRatio = baselineDuration / DurationMs;
            }
        }
    }
}